<template>

  <div style="margin:40px;">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="margin:0;font-size:18px;font-weight:bold;">{{ this.$route.meta.title }}</span>
        <el-button style="float: right; padding:3px 0" type="text" @click="cancelForm">返回列表</el-button>
      </div>
      <div style="max-width:1000px;margin:auto;">
        <el-form
          ref="form"
          :model="form"
          :rules="form.houseType==0?rulesPesonal:rules"
          class="table-form"

          size="small"
        >
          <table class="table table-bordered th-bg">
            <tbody>
            <tr>
              <th width="15%">企业名称</th>
              <td colspan="3">
                <el-form-item prop="enterpriseName">
                  <el-input v-model="form.enterpriseName"/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th width="15%">认定高企年度</th>
              <td width="35%">
                <el-form-item prop="recognizeYear">
                  <el-date-picker 
                    v-model="form.recognizeYear" 
                    clearable size="small" 
                    type="year" 
                    placeholder="请选择认定高企年度" 
                    value-format="yyyy" 
                  >
                  </el-date-picker>
                </el-form-item>
              </td>
              <th width="15%">备注</th>
              <td width="35">
                <el-form-item prop="notes">
                  <el-input v-model="form.notes"/>
                </el-form-item>
              </td>
            </tr>
            </tbody>
          </table>
          <div style="display:flex; align-item: center; justify-content: center; margin-top:20px;">
            <el-button
              :loading="loading"
              type="primary"
              size="small"
              @click="submitForm"
            >
              {{isEdit ? '更新高新技术企业' : '新增高新技术企业'}} 
            </el-button>            
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import crudHighTechnology from '@/api/business/enterprise/highTechnology'
import { MessageBox } from 'element-ui'
import { landStatusList, stageList, categoryList, levelList } from '@/utils/index.js'
const defaultForm = {
  
}

export default {
  components: {
  },
  computed: {
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      landStatusList,
      stageList,
      categoryList,
      levelList,
      form: JSON.parse(JSON.stringify(defaultForm)),
      rules: {
        enterpriseName: [
          {
            required: true,
            message: '请输入企业名称'
          }
        ],
        recognizeYear: [
          {
            required: true,
            message: '请选择认定高企年度'
          }
        ],
      },  
      loading: false,
      id: null,
    }
  },

  created() {
    if (this.isEdit) {
      this.id = this.$route.params && this.$route.params.id
      this.fetchData(this.id)
    }else {
    }
  },
  computed: {
    
  },
  methods: {
    fetchData(id) {
      crudHighTechnology.get(id).then(response => {
        this.form = Object.assign({}, this.form, response.data)
    
        // if (response.data.fireFileList) {
        //   for (let i = 0; i < response.data.fireFileList.length; i++) {
        //     const item = response.data.fireFileList[i]
        //     const file = {
        //       fileId: item.fileId,
        //       name: item.fileName,
        //       url: `business/file/download?fileId=${item.fileId}`
        //     }
        //     this.fireFileList.push(file)
        //   }
        //   this.form.fireFileIdList = this.fireFileList.map(x => x.fileId)
        // }
       
      }).catch(err => {
        console.log(err)
      })
    },
    cancelForm() {
      this.$router.push('/business/enterprise/highTechnology')
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        MessageBox.confirm('您确认执行此操作吗？', '确认操作', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          if (this.isEdit) {
            crudHighTechnology.edit(this.form).then(() => {
              this.$notify({
                title: '成功',
                message: '修改信息成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.cancelForm()
            }).catch(err => {
              console.log(err)
              this.loading = false
            })
          } else {
            crudHighTechnology.add(this.form).then(() => {
              this.$notify({
                title: '成功',
                message: '提交信息成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.cancelForm()
            }).catch(err => {
              console.log(err)
              this.loading = false
            })
          }
        }).catch((e) => {
          console.log(e)
        });
      })
    },
    mounted() {
      // 模拟 ajax 请求，异步渲染编辑器
      setTimeout(() => {
        this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
      }, 1500)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}

.amap-wrapper {
  width: 500px;
  height: 500px;
}

::v-deep .map-dialog .el-dialog__body {
  padding: 10px 20px;
}

</style>
<style src="@wangeditor/editor/dist/css/style.css"></style>